package ${package.application}.service;

import ${package.application}.api.assembler.${entity}DTOAssembler;
import ${package.application}.api.request.${entity}CreateRequest;
import ${package.application}.api.request.${entity}QueryRequest;
import ${package.application}.api.request.${entity}UpdateRequest;
import ${package.application}.api.response.${entity}QueryResponse;
import ${package.application}.api.response.${entity}Response;
import ${package.application}.repository.I${entity}ReadModelRepo;
import ${package.domain}.entity.${entity};
import ${package.domain}.service.${entity}Service;
import ${package.common}.base.ApiPageResponse;
import ${package.common}.base.ApiResponse;
import ${package.common}.base.DomainResponse;
import ${package.common}.base.Pagination;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * ${table.comment!}应用服务
 *
 * @author ${author}
 * @since ${date}
 */
@Service
@AllArgsConstructor
public class ${entity}AppService {

    /**
     * ${table.comment!}领域服务
     */
    private final ${entity}Service ${entity?uncap_first}Service;

    /**
     * ${table.comment!}读模型资源库
     */
    private final I${entity}ReadModelRepo ${entity?uncap_first}ReadModelRepo;

    /**
     * ${table.comment!}数据传输对象转换器
     */
    private final ${entity}DTOAssembler ${entity?uncap_first}DTOAssembler;

    /**
     * 创建${table.comment!}
     *
     * @param createRequest ${table.comment!}创建对象
     * @return 响应结果
     */
    public ApiResponse<Void> create(${entity}CreateRequest createRequest) {
        // ${table.comment!}创建对象 转成 ${table.comment!}实体
        ${entity} ${entity?uncap_first} = ${entity?uncap_first}DTOAssembler.assembler(createRequest);
        // 调用创建${table.comment!}领域服务
        DomainResponse<Void> domainResponse = ${entity?uncap_first}Service.create(${entity?uncap_first});
        // 领域响应结果转成API响应结果
        return ApiResponse.valueOf(domainResponse);
    }

    /**
     * 根据主键删除${table.comment!}
     *
     * @param ${entity?uncap_first}Id ${table.comment!}主键
     * @return 响应结果
     */
    public ApiResponse<Void> deleteById(${idPropertyType} ${entity?uncap_first}Id) {
        // 调用删除${table.comment!}领域服务
        DomainResponse<Void> domainResponse = ${entity?uncap_first}Service.deleteById(${entity?uncap_first}Id);
        // 领域响应结果转成API响应结果
        return ApiResponse.valueOf(domainResponse);
    }

    /**
     * 根据主键更新${table.comment!}
     *
     * @param ${entity?uncap_first}Id ${table.comment!}主键
     * @param updateRequest ${table.comment!}更新对象
     * @return 响应结果
     */
    public ApiResponse<Void> updateById(${idPropertyType} ${entity?uncap_first}Id, ${entity}UpdateRequest updateRequest) {
        // ${table.comment!}更新对象 转成 ${table.comment!}实体
        ${entity} ${entity?uncap_first} = ${entity?uncap_first}DTOAssembler.assembler(updateRequest);
        // 设置主键
        ${entity?uncap_first}.setId(${entity?uncap_first}Id);
        // 调用更新${table.comment!}领域服务
        DomainResponse<Void> domainResponse = ${entity?uncap_first}Service.updateById(${entity?uncap_first});
        // 领域响应结果转成API响应结果
        return ApiResponse.valueOf(domainResponse);
    }

    /**
     * 根据主键查询${table.comment!}详情
     *
     * @param ${entity?uncap_first}Id ${table.comment!}主键
     * @return 响应结果
     */
    public ApiResponse<${entity}Response> queryById(${idPropertyType} ${entity?uncap_first}Id) {
        // 根据主键查询${table.comment!}
        DomainResponse<${entity}> domainResponse = ${entity?uncap_first}Service.queryById(${entity?uncap_first}Id);
        // 领域响应结果转成API响应结果
        return ApiResponse.valueOf(domainResponse, ${entity?uncap_first}DTOAssembler::toResponse);
    }

    /**
     * 分页查询${table.comment!}
     *
     * @param pagination   分页
     * @param queryRequest ${table.comment!}查询对象
     * @return 响应结果
     */
    public ApiPageResponse<${entity}QueryResponse> query(Pagination pagination, ${entity}QueryRequest queryRequest) {
        // 调用${table.comment!}读模型资源库分页查询
        List<${entity}QueryResponse> list = ${entity?uncap_first}ReadModelRepo.query(pagination, queryRequest);
        return ApiPageResponse.ok(pagination, list);
    }
}
